﻿@using System.Web.Http
@using System.Web.Http.Controllers
@using System.Web.Http.Description
@using System.Collections.ObjectModel
@using WebFoundation.WebServer.Areas.HelpPage
@using WebFoundation.WebServer.Areas.HelpPage.Models
@using WebFoundation.WebServer.Areas.HelpPage.ModelDescriptions
@model Collection<ApiDescription>

@{
    // Group APIs by controller
    ILookup<HttpControllerDescriptor, ApiDescription> apiGroups = Model.ToLookup(api => api.ActionDescriptor.ControllerDescriptor);
    Action<ModelDescription, Dictionary<ModelDescription, MvcHtmlString>> createCode = null;

    Action<ModelDescription, Dictionary<ModelDescription, MvcHtmlString>> funcGetDefinedCode = (ModelDescription desc, Dictionary<ModelDescription, MvcHtmlString> codeList) =>
        {
            if (desc.ModelType == typeof(Object) || desc.ModelType == typeof(IHttpActionResult))
            {
                return;
            }

            if (desc is ComplexTypeModelDescription)
            {
                if (!codeList.ContainsKey(desc))
                {
                    codeList.Add(desc, Html.DisplayFor(m => desc, "DisplayTemplates/Parameters"));
                    foreach (var item in ((ComplexTypeModelDescription)desc).Properties)
                    {
                        createCode(item.TypeDescription, codeList);
                    }
                }
            }
            else if (desc is CollectionModelDescription)
            {
                createCode(((CollectionModelDescription)desc).ElementDescription, codeList);
            }
            else if (desc is EnumTypeModelDescription)
            {

                if (!codeList.ContainsKey(desc))
                {
                    codeList.Add(desc, Html.DisplayFor(m => desc, "DisplayTemplates/EnumTypeModelDescription"));
                }
            }
            else if (desc is DictionaryModelDescription)
            {
                var dicdesc = desc as DictionaryModelDescription;
                createCode(dicdesc.KeyModelDescription, codeList);
                createCode(dicdesc.ValueModelDescription, codeList);
            }
            else if (desc is KeyValuePairModelDescription)
            {
                var dicdesc = desc as KeyValuePairModelDescription;
                createCode(dicdesc.KeyModelDescription, codeList);
                createCode(dicdesc.ValueModelDescription, codeList);
            }
        };
    createCode = funcGetDefinedCode;

    HashSet<string> names = new HashSet<string>();
}
// THIS CODE WAS GENERATED BY API SERVICE, IT WILL TAKE ERROR IF CHANGE IT!!!!
// if you found any error, please re-generate it by ~/apiscripts/typescript/
// Web foundation Service Api.
/* tslint:disable:max-classes-per-file */
/* tslint:disable:max-line-length */

import DhiApp from '@@/generic/DhiApp';

export type Float = number;
export type Double = number;
export type Single = number;
export type Guid = string;
export type DateTime = string;
export type Char = string;
export type Decimal = number;
export type SByte = number;
export type Byte = number;
export type Int16 = number;
export type UInt16 = number;
export type Int32 = number;
export type UInt32 = number;
export type Int64 = number;
export type UInt64 = number;
export type String = string;
export type Boolean = boolean;
export type IHttpActionResult = any;



@foreach (var groupitem in apiGroups)
{
    <text>
        @foreach (var api in groupitem)
        {
            var sumod = ((HttpConfiguration)ViewBag.Config).GetHelpPageApiModel(api.GetFriendlyId());

            Dictionary<ModelDescription, MvcHtmlString> predefinedCodes = new Dictionary<ModelDescription, MvcHtmlString>();
            if (sumod.ResourceProperties != null)
            {
                foreach (var proItem in sumod.ResourceProperties)
                {
                    // proItem.TypeDescription
                    createCode(proItem.TypeDescription, predefinedCodes);
                }
            }

            if (sumod.RequestBodyParameters != null && sumod.RequestBodyParameters.Count > 0)
            {
                foreach (var param in sumod.RequestBodyParameters)
                {
                    // proItem.TypeDescription
                    createCode(param.TypeDescription, predefinedCodes);
                }
            }

            if (sumod.RequestModelDescription != null)
            {
                createCode(sumod.RequestModelDescription, predefinedCodes);
            }

            if (sumod.ResourceDescription != null)
            {
                createCode(sumod.ResourceDescription, predefinedCodes);
            }

            if (sumod.UriParameters != null && sumod.UriParameters.Count > 0)
            {
                foreach (var item in sumod.UriParameters)
                {
                    createCode(item.TypeDescription, predefinedCodes);
                }
            }

            if (predefinedCodes.Count > 0)
            {
                foreach (var item in predefinedCodes)
                {
                    if (names.Contains(item.Key.Name))
                    {
                        continue;
                    }

                    if (!string.IsNullOrWhiteSpace(item.Key.Documentation))
                    {
                        @:/** @item.Key.Documentation */
                    }

                    names.Add(item.Key.Name);
                    if (item.Key is EnumTypeModelDescription)
                    {
                        @: export enum @item.Key.Name {
                        @item.Value
                        @:}
                    }
                    else
                    {
                        @: export interface @item.Key.Name {
                        @item.Value
                        @:}
                    }
                }
            }

            if (sumod.UriParameters != null && sumod.UriParameters.Count > 0)
            {

                @:/** @("Parameter of action " + api.ActionDescriptor.ControllerDescriptor.ControllerName + "Module." + api.ActionDescriptor.ActionName) */
                @:export interface @string.Join("", groupitem.Key.ControllerName + "Module", api.ActionDescriptor.ActionName)Parameters{

                @Html.DisplayFor(m => sumod.UriParameters, "Parameters")
                @:}
            }

        }

        export class @(groupitem.Key.ControllerName + "Module"){
        constructor(private app:DhiApp){
        }

        @*//sub functions*@
        @foreach (var api in groupitem)
        {
            Html.RenderPartial("./DisplayTemplates/HelpPageApiModel", ((HttpConfiguration)ViewBag.Config).GetHelpPageApiModel(api.GetFriendlyId()));
        }
        }
    </text>
}

@*export class ApiService extends Module{
@foreach (var groupitem in apiGroups)
{
    <text> @groupitem.Key.ControllerName :  @(groupitem.Key.ControllerName + "Module") ;</text>
}
    constructor(app:DhiApp){
        super(app);
@foreach (var groupitem in apiGroups)
{
    <text> this.@groupitem.Key.ControllerName = new @(groupitem.Key.ControllerName + "Module") (app);</text>
}
    }
}*@